package cn.yshujia.controller;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Album;
import cn.yshujia.domain.vo.AlbumVO;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.service.impl.AlbumServiceImpl;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author YShuJia
 * @create 2025/3/4
 * @description AlbumController
 */

@RestController
@Tag (name = "Album", description = "相册Api")
public class AlbumController {
	
	@Resource
	private AlbumServiceImpl service;
	
	@GetMapping ("/album/{id}")
	@Operation (summary = "通过ID 获取相册")
	public ApiVO<AlbumVO> list(@PathVariable Long id) {
		return ApiVO.success(service.selectById(id));
	}
	
	@GetMapping ("/album/page")
	@Operation (summary = "获取相册分页")
	public ApiVO<PageVO<AlbumVO>> list(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.page(dtoPage));
	}
	
	@GetMapping ("/admin/album/page")
	@Operation (summary = "admin 获取相册分页")
	@PreAuthorize ("@sys.hasOnePermission('album:all')")
	public ApiVO<PageVO<AlbumVO>> page(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(dtoPage));
	}
	
	
	@PostMapping ("/admin/album/insert")
	@Operation (summary = "admin 新增相册")
	@PreAuthorize ("@sys.hasOnePermission('album:all')")
	public ApiVO<Boolean> insert(@RequestParam ("files") MultipartFile[] files, Album album) {
		Boolean result = service.insert(files, album);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/admin/album/update")
	@Operation (summary = "admin 修改相册，可传入图片")
	@PreAuthorize ("@sys.hasOnePermission('album:all')")
	public ApiVO<Boolean> update(@RequestParam (value = "files", required = false) MultipartFile[] files, Album album) {
		Boolean result = service.update(files, album);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@DeleteMapping ("/admin/album/delete")
	@Operation (summary = "admin 删除相册")
	@PreAuthorize ("@sys.hasOnePermission('album:all')")
	public ApiVO<Boolean> delete(@RequestBody List<Long> ids) {
		Boolean result = service.remove(ids);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}
